﻿@using OSBLE.Controllers;
@using OSBLE.Models.Courses;
@using OSBLE.Models
@model IEnumerable<OSBLE.Models.Courses.CourseUser>


@{
    ViewBag.Title = "Roster";
}

<style type="text/css">
    .disabled {
        color: #999;
    }

    .ui-dialog {
        position: fixed;
        right: 0;
    }
</style>

<script type="text/javascript" src="@Url.Content("~/Scripts/RosterScripts.js")"></script>

<script type="text/javascript">
    $(document).ready(function () {
        $("#deleteAllWithdrawnStudents").click(function () {
            r = confirm("Are you sure? This action is irreversible and all withdrawn student work will be lost");
            if (r) {
                return true;
            }
            else {
                return false;
            }
        });
    });
</script>

<div id="multipleSelectedAction">
    <!--Multiple selected Users actions-->
    <h3 id="multipleActionHeader"><br />User Actions<br /></h3>
    <h4 style="color:black;">Click on multiple users at once to perform actions on all selected users.</h4>
    <h4>Select all users: <input type="checkbox" title="Check this box to select / deselect all users" onchange="selectAll(this)" id="selectAllUsers" style="width: 40px; height: 20px;"> </h4>

    <h4>Selected User Actions:</h4>
    @if (!(ViewBag.ActiveCourse.AbstractCourse is Community)) {
        <text>Move to section: <br />(Ctrl Click to select multiple)</text>

        <select multiple id="sectionSelection" style="color:black;">
            <option value="-2">All Sections</option>
            <option value="0">0</option>
            @{
        string count = "1";
        bool flag = true;
        foreach (RosterController.UsersBySection usersBySection in ViewBag.UsersBySections)
        {
            if (usersBySection.SectionNumber != "0" && usersBySection.SectionNumber != "-1" && usersBySection.SectionNumber != "-2")
            {
                <option value="@usersBySection.SectionNumber">@usersBySection.SectionNumber</option>
                if (count == usersBySection.SectionNumber)
                {
                    count = (Int32.Parse(usersBySection.SectionNumber) + 1).ToString(); //alwasy set count to the highest number
                }
            }
        }
        <option value="@count">New Section (@count)</option>
            }
        </select>
        <input type="button" id="moverButton" onclick="moveUsers()" value="Move" />
        <br />
        <br />
    }

    @if (null != ViewBag.HideMail && !ViewBag.HideMail)
    {
        using (Html.BeginForm("CreateUserProfileIds", "Mail", FormMethod.Post, new { id = "emailForm", style = "display: inline;" }))
        {
            <input type="hidden" name="emailIDList" id="emailIDList" value="" />
            <text>Mail: </text>
            <a title="Mail Selected Users" onclick="massMail()">@Helpers.MailButton()</a>
            <text> | </text>
        }
    }

    @if (!(ViewBag.ActiveCourse.AbstractCourse is Community)) {

        if (ViewBag.ActiveCourseUser != null && ViewBag.ActiveCourseUser.AbstractRoleID == (int)CourseRole.CourseRoles.Instructor)
        {
            using (Html.BeginForm("ChangeStudentsToWithdrawnRole", "Roster", FormMethod.Post, new { id = "withdrawForm", style = "display: inline;" }))
            {
                <input type="hidden" name="withdrawIDList" id="withdrawIDList" value="" />
                <text> Withdraw: </text>
                <a title="Withdraw selected students" onclick="massWithdraw()">@Helpers.WithdrawButton()</a>
            }

            using (Html.BeginForm("ChangeWithdrawnUsersToStudentRole", "Roster", FormMethod.Post, new { id = "enrollForm", style = "display: inline;" }))
            {
                <input type="hidden" name="enrollIDList" id="enrollIDList" value="" />
                <text> | Enroll: </text>
                <a title="Enroll selected users" onclick="massEnroll()">@Helpers.EnrollButton()</a>
            }

            using (Html.BeginForm("removeSelectedUsers", "Roster", FormMethod.Post, new { id = "removeForm", style = "display: inline;" }))
            {
                <input type="hidden" name="removeIDList" id="removeIDList" value="" />
                <text> | Remove withdrawn: </text>
                <a title="Remove selected students" onclick="massRemove()">@Helpers.DeleteButton()</a>
            }
        }
    }
    else
    {
        using (Html.BeginForm("removeSelectedUsersCommunity", "Roster", FormMethod.Post, new { id = "removeForm", style = "display: inline;" }))
        {
            <input type="hidden" name="removeIDList" id="removeIDList" value="" />
            <text> | Remove users: </text>
            <a title="Remove selected users" onclick="massRemove()">@Helpers.DeleteButton()</a>
        }
        //remove community user , check for if you're removing yourself.

    }
    <br />
    <br />
</div>




<h2> Roster</h2>
<h5><span class="glyphicon glyphicon-info-sign"></span> Need help on how to use the Roster? Check out the OSBLE Help page <a href="~/help/RosterGuide" class="Hashtag"><u>"Managing your Course Roster"</u>.</a></h5>
<h4>@ViewBag.Notice</h4>
@if (ViewBag.ActiveCourse.AbstractRole.CanModify)
{
    if (!(ViewBag.ActiveCourse.AbstractCourse is Community))
    {
        <div>
            <a href="#" onclick="$('#add_update_users').toggle('blind'); return false;">Add/Update
                Users</a>
        </div>
        <div id="add_update_users">
            <h4>
                Add Single User @Helpers.CreateToolTip(ToolTips.RosterSingleUser)
            </h4>
            <div>
                @Html.ActionLink("Add By School ID", "Create")&nbsp;&nbsp;
                @Html.ActionLink("Add By Email", "CreateByEmail")&nbsp;&nbsp;
            </div>
            <h4>
                Import Roster @Helpers.CreateToolTip(ToolTips.RosterImport)
            </h4>
            <div>
                Upload a comma-separated (CSV) roster of students in this course:<br />
                @using (Html.BeginForm("ImportRoster", "Roster", FormMethod.Post, new { enctype = "multipart/form-data" }))
                {
                    <input id="import_roster_pick_file" type="file" name="file" accept=".csv" />
                    <input id="import_roster_submit" class="disabled" type="submit" value="Upload Roster" disabled="disabled" />
                    <p id="import_roster_error" class="error" hidden="hidden">Currently OSBLE+ only accepts comma-separated ('.csv') files. Please choose a roster in the .csv format and try again.</p>
                }
            </div>
        </div>
    }
    else
    {
        <div>
            @Html.ActionLink("Add By Email", "CreateByEmail")&nbsp;&nbsp;
        </div>
    }
}
<div>
    <br />
    Quick Filter By Name: @Helpers.CreateToolTip(ToolTips.RosterQuickFilter)
    <input type="text" id="roster_find" />

</div>

<div>


    <br />
</div>



@* Hides anything that doesn't match search filter *@
<script type="text/javascript">
    if ("Cat".match(".*as")) alert('hi');
    $('#roster_find').keyup(function () {
        // Get search string and remove all non-alphanumeric characters (or dashes)
        var search = $('#roster_find').val().toLowerCase().replace(/[^a-z0-9\-\ ]*/gi, "");

        // Animation function. tries to avoid overlap.
        $('.roster_name').each(function () {
            if ($(this).html().toLowerCase().match(search)) {
                if (($(this).parent().data("showing") != true) && ($(this).parent().css("display") == "none")) {
                    $(this).parent().data("showing", true);
                    $(this).parent().show('highlight', {}, "slow", function () { $(this).data("showing", false); });
                }
            } else {
                if (($(this).parent().data("hiding") != true) && ($(this).parent().css("display") != "none")) {
                    $(this).parent().data("hiding", true);
                    $(this).parent().hide('highlight', {}, "slow", function () { $(this).data("hiding", false); });
                }
            }
        });
    });
</script>

<script>
    function RosterPillButtonPressed(roleName, section)
    {
        $('td.section-' + section).removeClass("active");
        $('td.section-' + section + '.role-' + roleName).addClass("active");
        $('.roster-btn.section-' + section).removeClass("btn-primary");
        $('.roster-btn.section-' + section + '.role-' + roleName).addClass("btn-primary");
    }

    $(document).ready(function () {
        $('.roster_table td:first-child').addClass("active");
    });

    $('#import_roster_pick_file').change(function () {
        var fileExtension = ['csv']; //TODO: change here when support for other files types are added
        if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
            $("#import_roster_submit").prop('disabled', true);
            $("#import_roster_submit").addClass("disabled");
            $("#import_roster_error").prop('hidden', false);
        }
        else {
            $("#import_roster_submit").prop('disabled', false);
            $("#import_roster_submit").removeClass("disabled");
            $("#import_roster_error").prop('hidden', true);
        }
    });
</script>

@{
    bool whitelistFlag = true;

    foreach (RosterController.UsersBySection usersBySection in ViewBag.UsersBySections)
    {
        <div class="sectionDiv" section="@usersBySection.SectionNumber">

            @if (!(ViewBag.ActiveCourse.AbstractCourse is Community))
            {
                if (usersBySection.SectionNumber == "-1")
                {
                    <h3 style="display:inline;">Multiple Section Users</h3>
                }
                else if(usersBySection.SectionNumber == "-2")
                {
                    <h3 style="display:inline;">Teaching Personnel</h3>
                }
                else
                {
                    <h3 style="display:inline;">@Helpers.DisplaySection(usersBySection.SectionNumber)
                    </h3>
                }


                <input type="checkbox" title="Check this box to select / deselect all users in this section" id="sectionCheck" class="sectionCheckBox" onchange="sectionCheckChange(this)" section="@usersBySection.SectionNumber" style="width: 40px; height: 20px;" />
            }
            else
            {
                <input type="checkbox" title="Check this box to select / deselect all users in this section" id="sectionCheck" class="sectionCheckBox" onchange="sectionCheckChange(this)" section="@usersBySection.SectionNumber" style="display: none;" />
            }
            @if (usersBySection.SectionNumber == "-2")
            {
                <h2 id="SelectHelper" style="display:inline;">Click on users to perform actions on them</h2>
            }

            @* Button Pills used to simplify navigation on mobile devices (only visible when width < 850px)*@
            <div class="btn-group btn-group-justified roster_pills">
                @{ bool firstRole = true; }
                @foreach (RosterController.UsersByRole ubr in usersBySection.UsersByRole)
                {
                    if (ubr.Count > 0)
                    {
                        <div class="btn-group">
                            <button type="button" class="roster-btn role-@ubr.RoleName section-@usersBySection.SectionNumber btn@(firstRole ? " btn-primary" : "")" onclick="RosterPillButtonPressed('@ubr.RoleName' , @usersBySection.SectionNumber )">
                                @ubr.RoleName
                            </button>
                        </div>

                        if (firstRole)
                        {
                            firstRole = false;
                        }
                    }
                }
            </div>

            <table class="roster_table">
                <tr>
                    @foreach (RosterController.UsersByRole userByRole in usersBySection.UsersByRole)
                    {
                        //yc: going to need to add a check for users with editing privlidges on this portion too
                        //the only person who should see the white list is instructors and ta's. TA's cannot modify
                        if (userByRole.RoleName == "Withdrawn" && ViewBag.WhiteTableUsers.Count != 0)
                        {
                            <td class="role-Withdrawn section-@usersBySection.SectionNumber" id="OriginalWhiteListData">
                                <!--Prints the users in the whitetable for the current class-->

                                @if (whitelistFlag) //only want to display white list one time.
                                {
                                    <h3>
                                        Whitelisted(<span id="roleheader_@(usersBySection.SectionNumber)_whitetable">@ViewBag.WhiteTableUsers.Count.ToString()</span>)
                                        @if (ViewBag.ActiveCourse.AbstractRole.CanModify)
                                        {
                                            <a title="Clear All Whitelisted Users" href="@Url.Action("BatchClearWhiteTable", "Roster")">@Helpers.DeleteButton()</a>
                                            <a title="Resend Invitation Email" href="@Url.Action("BatchEmailWhiteTable", "Roster")">@Helpers.MailButton()</a>
                                        }
                                        @Helpers.CreateToolTip(ToolTips.RosterWhitetableRoleRole)
                                    </h3>

                                    <ul class="user_list">
                                        @foreach (var WTuser in ViewBag.WhiteTableUsers)
                                        {
                                            if (WTuser.Email != String.Empty && WTuser.Email != null)
                                            {
                                                <li id="wtuser_item_@WTuser.ID">
                                                    <div class="user_controls">
                                                        <a title="Send Course Invitation Email" href="@Url.Action("resendWhiteTableEmail", "Roster", new { wtUserId = WTuser.ID })">@Helpers.MailButton()</a>

                                                        @if (ViewBag.ActiveCourse.AbstractRole.CanModify)
                                                        {
                                                            <a title="Change Role / Inspect User" href="@Url.Action("EditWTUser", "Roster", new { wtuID = WTuser.ID })">@Helpers.EditButton()</a>
                                                            <a title="Delete This User" href="@Url.Action("DeleteWTUser", "Roster", new { wtuID = WTuser.ID })">@Helpers.DeleteButton()</a>
                                                        }
                                                    </div>
                                                    @* Whitelisted users do not have an osble account and therefore cannot have a profile picture yet! *@
                                                    @Helpers.SmallProfilePicture(@Url.Action("ProfilePicture", new { userProfile = -1 }))
                                                    <span>@WTuser.Name1 @WTuser.Name2 </span>
                                                </li>
                                            }

                                            else if (WTuser.Email == String.Empty || WTuser.Email == null)
                                            {
                                                <li class="wt_no_email">
                                                    <div class="user_controls">
                                                        @if (ViewBag.ActiveCourse.AbstractRole.CanModify)
                                                        {
                                                            <a title="Change Role / Inspect User" href="@Url.Action("EditWTUser", "Roster", new { wtuID = WTuser.ID })">@Helpers.EditButton()</a>
                                                            @*<a title="Delete This User" href="@Url.Action("DeleteWTUser", "Roster", new { wtuID = WTuser.ID })">@Helpers.DeleteButton()</a>*@
                                                        }
                                                    </div>
                                                    @* Whitelisted users do not have an osble account and therefore cannot have a profile picture yet! *@
                                                    @Helpers.SmallProfilePicture(@Url.Action("ProfilePicture", new { userProfile = -1 }))
                                                    <span>@WTuser.Name1 @WTuser.Name2</span>
                                                </li>
                                            }
                                        }
                                    </ul>
                                        whitelistFlag = false; //only display this one time.
                                }
                            </td>
                        }
                        if (userByRole.Count > 0 && userByRole.RoleName != "Pending")
                        {
                            <td class="role-@userByRole.RoleName section-@usersBySection.SectionNumber">
                                <h3 id="sectionRoleHeader">
                                    @userByRole.RoleName (<span id="roleheader_@(usersBySection.SectionNumber)_@(userByRole.RoleName)">@userByRole.Count.ToString()</span>)
                                    @if (userByRole.RoleName == "Student" && ViewBag.ActiveCourse.AbstractRole.CanModify)
                                    {
                                        //<span id="withdrawAllStudents"><a title="Withdraw All Students" href="@Url.Action("BatchWithdraw", "Roster")">@Helpers.WithdrawButton()</a></span>

                                    }
                                    else if (userByRole.RoleName == "Withdrawn" && ViewBag.ActiveCourse.AbstractRole.CanModify)
                                    {
                                        @*<span id="deleteAllWithdrawnStudents">
                                                <a href="@Url.Action("BatchDeleteWithdrawn", "Roster")" title=" delete all withdrawn users">@Helpers.DeleteButton()</a>
                                            </span>*@
                                    }
                                    @try
                                    {
                                        @Helpers.CreateToolTip(ToolTips.RosterRolesDictionary[userByRole.RoleName]);
                                    }
                                    catch (KeyNotFoundException e)
                                    {
                                        @Helpers.CreateToolTip("Key not found: " + e);
                                    }
                                    <input type="checkbox" title="Check this box to select / deselect all users below" id="sectionRoleCheckAll" class="sectionRoleCheckBox" onchange="roleClicked(this)" section="@usersBySection.SectionNumber" style="width: 40px; height: 20px;" />
                                </h3> <!--Flag header area-->
                                <ul class="user_list">
                                    @foreach (OSBLE.Models.Users.UserProfile user in userByRole.Users)
                                    {
                                        <li onclick="blockClicked(this)" id="user_item_@(user.ID)" class="UserLI" section="@usersBySection.SectionNumber">
                                            @if ((user != ViewBag.CurrentUser) || ViewBag.CanEditSelf == true)
                                            {
                                                <div class="user_controls">

                                                    @if (userByRole.RoleName == "Withdrawn" && ViewBag.ActiveCourse.AbstractRole.CanModify)
                                                    {
                                                        @*<a title="Re-enroll as student" href="@Url.Action("ChangeWithdrawnToStudentRole", "Roster", new { userProfileID = user.ID })">@Helpers.EnrollButton()</a>*@
                                                    }

                                                    @if (user.UserName != null && ViewBag.CurrentUser.ID != user.ID)
                                                    {
                                                        @*<a title="Mail This User" href="@Url.Action("CreateUserProfileId", "Mail", new { id = user.ID })">@Helpers.MailButton()</a>*@
                                                    }

                                                    @if (ViewBag.ActiveCourse.AbstractRole.CanModify && ViewBag.CurrentUser.ID != user.ID)
                                                    {
                                                        <a title="Change Role / Inspect User" href="@Url.Action("Edit", "Roster", new { userProfileID = user.ID })">@Helpers.EditButton()</a>
                                                        @*<form action="@Url.Action("Delete")"
                                                            style="display: inline;"
                                                            data-ajax="true"
                                                            data-ajax-success="$('#user_item_@(user.ID)').hide('highlight',{},'slow',function(){
                                                                    $('#roleheader_@(usersBySection.SectionNumber)_@(userByRole.RoleName)').html(
                                                                        parseInt($('#roleheader_@(usersBySection.SectionNumber)_@(userByRole.RoleName)').html())-1);
                                                                        if(parseInt($('#roleheader_@(usersBySection.SectionNumber)_@(userByRole.RoleName)').html()) == 0) {
                                                                            $(this).parent().parent().remove();
                                                                        }
                                                                    $(this).remove();
                                                                });"
                                                            data-ajax-confirm="Are you sure you want to remove this user from the course?"
                                                            method="post">
                                                                        <input type="hidden" name="userProfileID" value="@user.ID" />
                                                                        @Helpers.DeleteSubmit("Remove This User")
                                                                        </form>*@

                                                        if (userByRole.RoleName == "Student")
                                                        {
                                                            @*<a title="Withdraw student" href="@Url.Action("ChangeStudentToWithdrawnRole", "Roster", new { userProfileID = user.ID })">@Helpers.WithdrawButton()</a>*@
                                                        }
                                                    }
                                                </div>
                                            }
                                            @Helpers.SmallProfilePicture(@Url.Action("ProfilePicture", new { userProfile = user.ID }))
                                            <span class="roster_name">@Helpers.DisplayFullNameOrPending(user)</span>
                                            <data id="userID" studentID="@user.ID"></data>
                                            <br />
                                            @if (usersBySection.SectionNumber == "-1") //if multiple section user, show current sections
                                            {
                                                <span id="SectionList">Sections: </span>
                                            }
                                            @*else if (usersBySection.SectionNumber == "-2")
                                                {
                                                    <span id="SectionList">All Sections</span>
                                                }*@

                                            <input id="moveStudentBox" type="checkbox" style="visibility:hidden;" class="moveThisUser" />
                                        </li>
                                    }
                                </ul>
                            </td>
                        }

                        else if (userByRole.Count > 0 && userByRole.RoleName == "Pending")
                        {
                            <td class="role-@userByRole.RoleName section-@usersBySection.SectionNumber">
                                <h3>
                                    @userByRole.RoleName (<span id="roleheader_@(usersBySection.SectionNumber)_@(userByRole.RoleName)">@userByRole.Count.ToString()</span> )
                                    @if (ViewBag.ActiveCourseuser != null && ViewBag.ActiveCourseUser.AbstractRoleID == (int)CourseRole.CourseRoles.Instructor)
                                    {
                                        <a title="Approve ALL Pending Users" href="@Url.Action("BatchApprove", "Roster")">@Helpers.ThumbsUpButton()</a>
                                        <a title="Deny ALL Pending Users" href="@Url.Action("BatchDeny", "Roster")">@Helpers.ThumbsDownButton()</a>
                                    }
                                    @try
                                    {
                                        @Helpers.CreateToolTip(ToolTips.RosterRolesDictionary[userByRole.RoleName]);
                                    }
                                    catch (KeyNotFoundException e)
                                    {
                                        @Helpers.CreateToolTip("Key not found: " + e);
                                    }
                                </h3>
                                <ul class="user_list">
                                    @foreach (OSBLE.Models.Users.UserProfile user in userByRole.Users)
                                    {
                                        <li id="user_item_@(user.ID)">
                                            @if ((user != ViewBag.CurrentUser) || ViewBag.CanEditSelf == true)
                                            {
                                                <div class="user_controls">
                                                    @if (ViewBag.ActiveCourse.AbstractRole.CanModify)
                                                    {
                                                        <a title="Approve this User" href="@Url.Action("ApprovePending", "Roster", new { userId = user.ID })">@Helpers.ThumbsUpButton()</a>
                                                        <a title="Deny this User" href="@Url.Action("DenyPending", "Roster", new { userId = user.ID })">@Helpers.ThumbsDownButton()</a>
                                                    }
                                                </div>
                                            }
                                            @Helpers.SmallProfilePicture(@Url.Action("ProfilePicture", new { userProfile = user.ID }))
                                            <span class="roster_name">@Helpers.DisplayFullNameOrPending(user)
                                            </span>
                                        </li>
                                    }
                                </ul>
                            </td>
                        }
                    }
                </tr>

            </table>
        </div>
    }
    
    
    
    <table id="WhiteListTable">
        <tr id="WhiteListRow">

        </tr>
    </table>
    <br />
    <br />
}
<script type="text/javascript">
    
    $(document).ready(function () {
        $("#WhiteListRow").append($('#OriginalWhiteListData').contents());
    });

</script>